Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Global API Key (X-Auth-Key) support to Wrangler #1351

Merged
merged 3 commits into from
Jun 27, 2022

Conversation

geelen
Copy link
Contributor

@geelen geelen commented Jun 24, 2022

Closes #1072

  • A user must supply both an Global API Key and Email for this to work.
  • Can use CLOUDFLARE_API_KEY and CLOUDFLARE_EMAIL or the deprecated CF_API_KEY and CF_EMAIL keys.
  • Takes precedence over CLOUDFLARE_API_TOKEN and oAuth.

Refactored the assumption that an apiToken was a string and have replaced that with an ApiCredentials type:

export type ApiCredentials =
  | {
      apiToken: string;
    }
  | {
      authKey: string;
      authEmail: string;
    };

This flowed fairly easily through the code, picked up inside addAuthorizationHeaderIfUnspecified:

function addAuthorizationHeaderIfUnspecified(
  headers: Record<string, string>,
  auth: ApiCredentials
): void {
  if (!("Authorization" in headers)) {
    if ("apiToken" in auth) {
      headers["Authorization"] = `Bearer ${auth.apiToken}`;
    } else {
      headers["X-Auth-Key"] = auth.authKey;
      headers["X-Auth-Email"] = auth.authEmail;
    }
  }
}

I chose to preserve the semantics that if the code has injected a specific Authorization header, we should send it on rather than replacing it with the global Key/Email (since it's used for time-limited JWTs for Pages uploads)

@changeset-bot
Copy link

changeset-bot bot commented Jun 24, 2022

🦋 Changeset detected

Latest commit: 42f053a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@geelen geelen force-pushed the global-auth-support branch from bd2680f to ea12efd Compare June 24, 2022 16:02
@github-actions
Copy link
Contributor

github-actions bot commented Jun 24, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2568855771/npm-package-wrangler-1351

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/1351/npm-package-wrangler-1351

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/2568855771/npm-package-wrangler-1351 dev path/to/script.js

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks for adding tests too! I think we should remove the GLOBAL / Global word everywhere, but I'm going to stamp it anyway. Feel free to land it after removing Global.

});
});

it("should use a Global API Key in preference to an API key", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it("should use a Global API Key in preference to an API key", async () => {
it("should use a Global API Key in preference to an API token", async () => {

variableName: "CLOUDFLARE_API_TOKEN",
deprecatedName: "CF_API_TOKEN",
});
const getCloudflareGlobalAuthKeyFromEnv = getEnvironmentVariableFactory({
variableName: "CLOUDFLARE_GLOBAL_API_KEY",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variableName: "CLOUDFLARE_GLOBAL_API_KEY",
variableName: "CLOUDFLARE_API_KEY",

we can remove this GLOBAL prefix everywhere!

deprecatedName: "CF_API_KEY",
});
const getCloudflareGlobalAuthEmailFromEnv = getEnvironmentVariableFactory({
variableName: "CLOUDFLARE_GLOBAL_API_EMAIL",
Copy link
Contributor

@threepointone threepointone Jun 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variableName: "CLOUDFLARE_GLOBAL_API_EMAIL",
variableName: "CLOUDFLARE_EMAIL",

return {
apiToken: usingGlobalAuthKey ? apiToken.authKey : apiToken.apiToken,
authType: usingGlobalAuthKey
? "Global API Key"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
? "Global API Key"
? "API Key"

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a changeset!

@geelen
Copy link
Contributor Author

geelen commented Jun 25, 2022

Happy to remove GLOBAL, only put it in because I'm not sure, otherwise, that the difference between a "key" and a "token" is particularly obvious to people. That and the Dash UI looks like this:

image

Thoughts?

@threepointone
Copy link
Contributor

The other vars are basically the older ones but with CF replaced with CLOUDFLARE. Since these are usually typed out at the command line, I'd prefer them them to be shorter (honestly I like CF too, but coldfusion got there before us so we have to use CLOUDFLARE instead)

BUT. I don't feel strongly about this! Feel free to take a call. I'll stamp this once you add a changeset tho.

@threepointone
Copy link
Contributor

Taking this over, will review and land. Thank you so much @geelen!

@threepointone threepointone merged commit c770167 into main Jun 27, 2022
@threepointone threepointone deleted the global-auth-support branch June 27, 2022 11:39
@github-actions github-actions bot mentioned this pull request Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: support CF_API_KEY
2 participants